/**
 * Copyright (C) 2009 SC 4ViewSoft SRL
 *  
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *  
 *      http://www.apache.org/licenses/LICENSE-2.0
 *  
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.jone.phone.detail.ui.chart;

import java.util.HashMap;
import java.util.Map;

import org.achartengine.ChartFactory;
import org.achartengine.GraphicalView;
import org.achartengine.renderer.DefaultRenderer;

import android.content.Context;
import android.database.Cursor;
import android.graphics.Color;
import android.provider.Contacts.People;
import android.util.Log;

import com.jone.phone.detail.model.TeleDetail;

/**
 * Budget demo pie chart.
 */
public class PieChart extends AbstractChart {
	
	
	private String projection[] = new String[] { People._ID, People.NAME, 
			People.NUMBER, People.PRIMARY_EMAIL_ID,   
			People.PRIMARY_ORGANIZATION_ID, People.
			PRIMARY_PHONE_ID, People.DISPLAY_NAME, People.TYPE };  
  /**
   * Returns the chart name.
   * @return the chart name
   */
  public String getName() {
    return "Budget chart";
  }
  
  /**
   * Returns the chart description.
   * @return the chart description
   */
  public String getDesc() {
    return "The budget per project for this year (pie chart)";
  }
  
  /** 
   * Executes the chart demo.
   * @param context the context
   * @return the built intent
   */
  public GraphicalView execute(Context context,String month) {
	Map<String,Double> map = getData(context,month);
	String[] keys = new String[map.size()];
    Double[] val = new Double[map.size()];
    map.keySet().toArray(keys);
    map.values().toArray(val);
    
    String[] persons = getPersons(context,keys);
    double[] values = convert(val);
    
    int[] colors = new int[map.size()];
    switch(map.size()){
    case 1:
    	colors = new int[] {Color.BLUE};
    	break;
    case 2:
    	colors = new int[] {Color.BLUE, Color.GREEN};
    	break;
    case 3:
    	colors = new int[] {Color.BLUE, Color.GREEN, Color.MAGENTA};
    	break;
    case 4:
    	colors = new int[] {Color.BLUE, Color.GREEN, Color.MAGENTA,Color.RED};
    	break;
    case 5:
    	colors = new int[] {Color.BLUE, Color.GREEN, Color.MAGENTA, Color.RED, Color.YELLOW};
    	break;
    }
    DefaultRenderer renderer = buildCategoryRenderer(colors);
    renderer.setLabelsColor(Color.WHITE);
    return  ChartFactory.getPieChartView(context, buildCategoryDataset("xxxxxxxxx", persons,values), renderer);
   
  }
  
  private Map<String,Double> getData(Context context,String month){
	  Map<String,Double> map = new HashMap<String,Double>();
	 
  	  String[] projection =  new String[] {"person_number", "SUM(duration) as duration"};
  	  String orderBy = "duration desc";
  	  String beginTime = month+"00000000";
  	  String endTime = month+"99999999";
  	  String selection = TeleDetail.TIME+">"+beginTime+" and "+TeleDetail.TIME+"<"+endTime;
	  Cursor c = context.getContentResolver().query(TeleDetail.URI_PERSON_COUNT, projection, selection, null, orderBy);
	  if(c!= null && c.moveToFirst()){
		  int count = 1;
		  while(!c.isAfterLast()){
			  if(count>5){
				  break;
			  }
			  String personNumber = c.getString(c.getColumnIndex(TeleDetail.PERSON_NUMBER));
			  Double value = c.getDouble(c.getColumnIndex(TeleDetail.DURATION));
			  map.put(personNumber, value);
			  c.moveToNext();
			  count++;
		  }
	  }
	  c.close();
	  //数据不够就补零
	  if(map.size()==0){
		  map.put("无数据", Double.valueOf(100));
	  }
	  return map;
  }
  
  private double[] convert(Double[] values){
	  double[] val = new double[values.length];
	  for(int i=0;i<values.length;i++){
		  val[i]=values[i];
		  
		  Log.d("Pie", "value:"+i+"="+values[i]);
	  }
	  return val;
  }
  
  private String[] getPersons(Context context,String[] keys){
	  String[] persons = new String[keys.length];
	  for(int i=0;i<keys.length;i++){
		  String selection = People.NUMBER+"='"+ keys[i]+"'";
		  Cursor c = context.getContentResolver().query(People.CONTENT_URI, projection, selection, null, null);
		  if(c.moveToFirst()){
			  persons[i] = c.getString(c.getColumnIndex(People.NAME));
		  }else{
			  persons[i] = keys[i];
		  }
		  c.close();
	  }
	  return persons; 
  }

}
